import Request from '@/utils/request';
import config from '@/config';

// 创建请求实例
const request = new Request({
  baseURL: config.baseUrl,
  timeout: 10000,
});

// 基础API
const api = {
  // 登录
  login(data) {
    console.log('调用登录API, 参数:', data);
    return request.post('/ms/login.do', data);
  },
  
  // 获取用户信息
  getUserInfo() {
    return request.get('/ms/user/info');
  }
};

// 分类相关接口
export const categoryApi = {
  // 获取分类列表
  getList: (params) => request.post('/ms/cms/category/list', params || {}),
  
  // 获取分类详情
  getDetail: (id) => request.get(`/ms/cms/category/get?id=${id}`),
  
  // 获取分类下的文章列表
  getArticles: (categoryId, params) => request.post(`/ms/cms/category/${categoryId}/articles`, params || {})
};

// 文章相关接口
export const articleApi = {
  // 获取文章列表
  getList: (params) => request.post('/ms/cms/content/list', params || {}),
  
  // 获取文章详情
  getDetail: (id) => request.get(`/ms/cms/content/get?id=${id}`),
  
  // 获取文章点击数
  getHit: (contentId) => request.get(`/ms/cms/content/${contentId}/hit`),
  
  // 搜索文章
  search: (params) => request.post('/ms/cms/content/search', params),
  
  // 获取推荐文章
  getRecommend: (params) => request.get('/ms/cms/content/recommend', params),
  
  // 获取热门文章
  getHot: (params) => request.get('/ms/cms/content/hot', params)
};

// 模板相关接口
export const templateApi = {
  // 获取模板列表
  getList: () => request.get('/ms/cms/template/list'),
  
  // 获取模板详情
  getDetail: (id) => request.get(`/ms/cms/template/get?id=${id}`),
  
  // 获取页面模板配置
  getPageTemplate: (pageType) => request.get(`/ms/cms/template/page/${pageType}`)
};

// 站点配置接口
export const siteApi = {
  // 获取站点信息
  getInfo: () => request.get('/ms/cms/site/info'),
  
  // 获取站点配置
  getConfig: () => request.get('/ms/cms/site/config'),
  
  // 获取导航菜单
  getMenu: () => request.get('/ms/cms/site/menu')
};

// 分享相关接口
export const shareApi = {
  // 创建分享
  createShare: (data) => request.post('/ms/cms/share/create', data),
  
  // 获取分享信息
  getShareInfo: (id) => request.get(`/ms/cms/share/info?id=${id}`),
  
  // 获取分享统计
  getShareStats: (id) => request.get(`/ms/cms/share/stats?id=${id}`),
  
  // 获取分类分享列表
  getCategoryShares: (params) => request.get('/ms/cms/category-share/list', params),

  // 获取分享的分类信息
  getSharedCategory: (params) => request.get('/ms/cms/category-share/category', params),

  // 获取分享的文章
  getSharedArticle: (params) => request.get('/ms/cms/share/article', params),
  
  // 记录分享浏览
  recordShareView: (shareId) => request.post('/ms/cms/share/view', { shareId }),

  // 验证分享是否有效
  validateShare: (token) => request.get(`/ms/cms/share/validate?token=${token}`)
};

// 导出API
export default api;
